package com.k.kpp.poi2;

import com.k.kpp.poi2.domain.Card;
import com.k.kpp.poi2.domain.Person;
import com.k.kpp.poi2.domain.ShiftInfo;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

public class POITest {
    public static void main(String[] args) {
        // list<project>
        try {
            excelWrite();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void excelWrite() throws IOException {
        List<Person> personList = getPersonData();
        List<Card> cardList = getCardData();
        List<ShiftInfo> shiftInfoList = getShiftInfoData();

        Workbook wb = WorkbookFactory.create(true);
        Sheet sheet = wb.createSheet("sheet1");
        // row0 column0
        Row row = sheet.createRow(0);
        Cell row0Column0 = row.createCell(0);
        row0Column0.setCellValue("");
        // row0 columnX
        for (int i = 0; i < cardList.size(); i++) {
            Cell row0ColumI = row.createCell(i + 1);
            Card card = cardList.get(i);
            row0ColumI.setCellValue(card.getPersonId());
        }
        // row1 columnX
        Row row1 = sheet.createRow(1);
        for (int i = 0; i < cardList.size(); i++) {
            Cell row0ColumI = row1.createCell(i + 1);
            Card card = cardList.get(i);
            row0ColumI.setCellValue(card.getName());
        }

        // 班次
        for (int i = 0; i < shiftInfoList.size(); i++) {
            sheet.createRow(i + 2).createCell(0).setCellValue(shiftInfoList.get(i).getName());
        }

        // 数据
        for (int i = 0; i < shiftInfoList.size(); i++) {
            sheet.getRow(i + 2).createCell(1);
        }

        // 合并
        sheet.addMergedRegion(new CellRangeAddress(0, 0, 1, 2));
        sheet.addMergedRegion(new CellRangeAddress(0, 0, 3, 5));

        String pathName = "E:/test/excelTest20240307.xlsx";
        try (FileOutputStream os = new FileOutputStream(pathName)) {
            wb.write(os);
        }
    }

    private static List<Person> getPersonData() {
        List<Person> personList = new ArrayList();
        Person p = new Person("p1", "pname1");
        Person p2 = new Person("p2", "pname2");
        personList.add(p);
        personList.add(p2);
        return personList;
    }

    private static List<Card> getCardData() {
        List<Card> cardList = new ArrayList<>();
        Card c = new Card("c1", "cname1", "p1");
        Card c2 = new Card("c2", "cname1", "p1");
        Card c3 = new Card("c3", "cname3", "p2");
        Card c4 = new Card("c4", "cname4", "p2");
        Card c5 = new Card("c5", "cname5", "p2");
        cardList.add(c);
        cardList.add(c2);
        cardList.add(c3);
        cardList.add(c4);
        cardList.add(c5);
        return cardList;
    }

    private static List<ShiftInfo> getShiftInfoData() {
        List<ShiftInfo> shiftInfoList = new ArrayList<>();
        ShiftInfo s = new ShiftInfo("s1", "sname1");
        ShiftInfo s2 = new ShiftInfo("s2", "sname2");
        ShiftInfo s3 = new ShiftInfo("s3", "sname3");
        ShiftInfo s4 = new ShiftInfo("s4", "sname4");
        shiftInfoList.add(s);
        shiftInfoList.add(s2);
        shiftInfoList.add(s3);
        shiftInfoList.add(s4);
        return shiftInfoList;
    }

    private void mergeRow(Sheet sheet, int startRow, int endRow, int cellNo) {
        if (endRow < startRow) {
            return;
        }
        String oldValue = sheet.getRow(startRow).getCell(cellNo).getStringCellValue();
        int index = startRow;
        for (int i = startRow + 1; i <= endRow; i++) {
            if (sheet.getRow(i).getCell(cellNo) != null) {
                String cellValue = sheet.getRow(i).getCell(cellNo).getStringCellValue();
                if (oldValue.equals(cellValue)) {
                    if (i == endRow) {
                        sheet.addMergedRegion(new CellRangeAddress(index, endRow, cellNo, cellNo));
                    }
                } else {
                    if (index != i - 1) {
                        sheet.addMergedRegion(new CellRangeAddress(index, i - 1, cellNo, cellNo));
                        oldValue = sheet.getRow(i).getCell(cellNo).getStringCellValue();
                        index = i;
                    } else {
                        oldValue = sheet.getRow(i).getCell(cellNo).getStringCellValue();
                        index = i;
                    }
                }
            }
        }
    }

    public void mergedColumn(Sheet xfSheet, int column, int row, int begin) {
        int regionStart = 1;
        Map<Integer, Integer> map = new LinkedHashMap<>();
        List<Map<Integer, Integer>> list = new ArrayList<>();
        for (int i = begin; i < xfSheet.getPhysicalNumberOfRows(); i++) {
            if (i < xfSheet.getLastRowNum()) {
                String secondIndex = xfSheet.getRow(i).getCell(column).getStringCellValue();
                String secondIndex2 = xfSheet.getRow(i + 1).getCell(column).getStringCellValue();
                if (!secondIndex.equals(secondIndex2)) {
                    map.put(regionStart, i);
                    regionStart = i + 1;
                }
            } else if (i == xfSheet.getLastRowNum()) {
                map.put(regionStart, i);
                regionStart = i + 1;
            }
        }
        list.add(map);
        for (Map<Integer, Integer> rmap : list) {
            for (Integer i : rmap.keySet()) {
                if (i >= row) {
                    if (!i.equals(rmap.get(i))) {
                        xfSheet.addMergedRegion(new CellRangeAddress(i, rmap.get(i), column, column));
                    }
                }
            }
        }
    }


}
